/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.internal.ui;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.leeloo.core.Tag;
import org.leeloo.core.TagService;
import org.leeloo.databinding.ui.event.FireBindingEventEvent;
import org.leeloo.internal.core.ObjectDescriptor;

/**
 * Viewer filter that filters items that are an exact match of a {@link Tag}.
 * 
 * @author Brad Reynolds
 */
public class TagFilter extends ViewerFilter {
    /**
     * Tag to filter against.
     */
    private final Tag tag;

    /**
     * Service for tag lookup.
     */
    private final TagService tagService;

    /**
     * Constructs a new instance making the provided <code>tagService</code>
     * the service to query for tags and the provided <code>tag</code> the
     * template to filter against.
     * 
     * @param tagService
     *            service to query against
     * @param template
     *            template to match to
     */
    public TagFilter(TagService tagService, Tag template) {
        if (tagService == null) {
            throw new IllegalArgumentException("Parameter 'tagService' was null."); //$NON-NLS-1$ 
        }
        if (template == null) {
            throw new IllegalArgumentException("Parameter 'tagService' was null."); //$NON-NLS-1$
        }

        this.tagService = tagService;
        this.tag = template;
    }

    /**
     * Filter on the "tags" property.
     * 
     * @see org.eclipse.jface.viewers.ViewerFilter#isFilterProperty(java.lang.Object,
     *      java.lang.String)
     */
    public boolean isFilterProperty(Object element, String property) {
        return "tags".equals(property); //$NON-NLS-1$
    }

    /**
     * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer,
     *      java.lang.Object, java.lang.Object)
     */
    public boolean select(Viewer viewer, Object parentElement, Object element) {
        boolean result = false;

        if (element instanceof FireBindingEventEvent) {
            FireBindingEventEvent event = (FireBindingEventEvent) element;

            ObjectDescriptor[] participants = event.getParticipants();
            for (int i = 0; !result && i < participants.length; i++) {
                ObjectDescriptor descriptor = participants[i];
                result = tag.equals(tagService.get(descriptor, tag.getName()));
            }
        }
        
        return result;
    }
}
